package com.ymatou.iapi.platformdiscounts.testcase;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.platformdiscounts.parameter.AddBean;
import com.ymatou.iapi.platformdiscounts.service.AddCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DiscountsCallService;
import com.ymttest.database.model.discount_qualification;
import com.ymttest.database.sqlwapper.discount_qualificationWapper;
import com.ymttest.utils.TestDataManager;
/**
*新增会员优惠资格
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_Add{
	private static AddBean bean;
	private static AddCall call;
	private discount_qualificationWapper dqWapper=new discount_qualificationWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("新增会员优惠资格");
	}

	@Before
	public void caseUp() {
		bean=new AddBean();
		call=new AddCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_add_001() {
		Logger.start(true,"正例-新增会员优惠资格");
		try {
			Date dNow = new Date(); // 当前时间
	        Date dBefore = new Date();
	        Date dAfter = new Date();
	        
	        Calendar calendar1 = Calendar.getInstance(); // 得到日历
	        calendar1.setTime(dNow);// 把当前时间赋给日历
	        calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
	        dBefore = calendar1.getTime(); // 得到前一天的时间

	        Calendar calendar2 = Calendar.getInstance(); // 得到日历
	        calendar2.setTime(dNow);// 把当前时间赋给日历
	        calendar2.add(Calendar.DAY_OF_MONTH, 10); // 设置为后十天
	        dAfter = calendar2.getTime(); // 得到前一天的时间
	        
	        Logger.comment("#####dBefore:#####"+dBefore);
	        Logger.comment("#####dAfter:#####"+dAfter);
			//int userId=TestDataManager.getRandomIntNum(5);
	       int userId=20226434;
			int sourceId=TestDataManager.getRandomIntNum(5);
			
			bean.setSourceId(sourceId);
			bean.setBizType(1);
			bean.setUserId(userId);
			//创建一个商品
			String productId=new DiscountsCallService().createProduct().getString("ProductId");
		//	String productId="8ea6944c-1fa1-428f-a753-0716f90e8cb0";
			bean.setProductId(productId);
			bean.setStartTime(dBefore);
			bean.setExpireTime(dAfter);
			
			bean.setAppId("11");
			bean.setRequestId("22");
			
			call.setData(bean);
			call.callService();			
			
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_add_002() {
		Logger.start(false,"反例-新增会员优惠资格-sourceId重复");
		try {
			Date dNow = new Date(); // 当前时间
	        Date dBefore = new Date();
	        Date dAfter = new Date();
	        
	        Calendar calendar1 = Calendar.getInstance(); // 得到日历
	        calendar1.setTime(dNow);// 把当前时间赋给日历
	        calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
	        dBefore = calendar1.getTime(); // 得到前一天的时间

	        Calendar calendar2 = Calendar.getInstance(); // 得到日历
	        calendar2.setTime(dNow);// 把当前时间赋给日历
	        calendar2.add(Calendar.DAY_OF_MONTH, 10); // 设置为后十天
	        dAfter = calendar2.getTime(); // 得到前一天的时间
	        
			int userId=TestDataManager.getRandomIntNum(5);
			//int sourceId=TestDataManager.getRandomIntNum(5);
			bean.setSourceId(1);
			bean.setBizType(1);
			bean.setUserId(userId);
			//创建一个商品
			String productId=new DiscountsCallService().createProduct().getString("ProductId");
			bean.setProductId(productId);
			bean.setStartTime(dBefore);
			bean.setExpireTime(dAfter);
			
			bean.setAppId("11");
			bean.setRequestId("22");
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("ok", call.getReturnData(), "验证返回ok");
			//验证数据库
			discount_qualification dq=dqWapper.selectTop1(Long.toString(bean.getSourceId()), Integer.toString(bean.getUserId()), bean.getProductId());
			Logger.verifyEquals(dq.getQualificationId(), null, "验证discount_qualification表插入不成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_add_003() {
		Logger.start(false,"反例-新增会员优惠资格-StartTime>ExpireTime");
		try {
			Date dNow = new Date(); // 当前时间
	        Date dBefore = new Date();
	        Date dAfter = new Date();
	        
	        Calendar calendar1 = Calendar.getInstance(); // 得到日历
	        calendar1.setTime(dNow);// 把当前时间赋给日历
	        calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
	        dBefore = calendar1.getTime(); // 得到前一天的时间

	        Calendar calendar2 = Calendar.getInstance(); // 得到日历
	        calendar2.setTime(dNow);// 把当前时间赋给日历
	        calendar2.add(Calendar.DAY_OF_MONTH, 10); // 设置为后十天
	        dAfter = calendar2.getTime(); // 得到前一天的时间
	        
			int userId=TestDataManager.getRandomIntNum(5);
			int sourceId=TestDataManager.getRandomIntNum(5);
			bean.setSourceId(sourceId);
			bean.setBizType(1);
			bean.setUserId(userId);
			//创建一个商品
			String productId=new DiscountsCallService().createProduct().getString("ProductId");
			bean.setProductId(productId);
			
			bean.setStartTime(dAfter);
			bean.setExpireTime(dBefore);
			
			bean.setAppId("11");
			bean.setRequestId("22");
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("ok", call.getReturnData(), "验证返回ok");
			//验证数据库
			discount_qualification dq=dqWapper.selectTop1(Long.toString(bean.getSourceId()), Integer.toString(bean.getUserId()), bean.getProductId());
			Logger.verifyEquals(dq.getQualificationId(), null, "验证discount_qualification表插入不成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_add_004() {
		Logger.start(false,"反例-新增会员优惠资格-sourceId为空");
		try {
			Date dNow = new Date(); // 当前时间
	        Date dBefore = new Date();
	        Date dAfter = new Date();
	        
	        Calendar calendar1 = Calendar.getInstance(); // 得到日历
	        calendar1.setTime(dNow);// 把当前时间赋给日历
	        calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
	        dBefore = calendar1.getTime(); // 得到前一天的时间

	        Calendar calendar2 = Calendar.getInstance(); // 得到日历
	        calendar2.setTime(dNow);// 把当前时间赋给日历
	        calendar2.add(Calendar.DAY_OF_MONTH, 10); // 设置为后十天
	        dAfter = calendar2.getTime(); // 得到前一天的时间
	        
			int userId=TestDataManager.getRandomIntNum(5);
			int sourceId=TestDataManager.getRandomIntNum(5);
			//bean.setSourceId(0);
			bean.setBizType(1);
			bean.setUserId(userId);
			//创建一个商品
			String productId=new DiscountsCallService().createProduct().getString("ProductId");
			bean.setProductId(productId);
			
			bean.setStartTime(dBefore);
			bean.setExpireTime(dAfter);
			
			bean.setAppId("11");
			bean.setRequestId("22");
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("fail", call.getReturnData(), "验证返回ok");
			//验证数据库
			discount_qualification dq=dqWapper.selectTop1(Long.toString(bean.getSourceId()), Integer.toString(bean.getUserId()), bean.getProductId());
			Logger.verifyEquals(dq.getQualificationId(), null, "验证discount_qualification表插入不成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Ts_add_005() {
		Logger.start(false,"反例-新增会员优惠资格-BizType不存在(BizType不做验证)");
		try {
			Date dNow = new Date(); // 当前时间
	        Date dBefore = new Date();
	        Date dAfter = new Date();
	        
	        Calendar calendar1 = Calendar.getInstance(); // 得到日历
	        calendar1.setTime(dNow);// 把当前时间赋给日历
	        calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
	        dBefore = calendar1.getTime(); // 得到前一天的时间

	        Calendar calendar2 = Calendar.getInstance(); // 得到日历
	        calendar2.setTime(dNow);// 把当前时间赋给日历
	        calendar2.add(Calendar.DAY_OF_MONTH, 10); // 设置为后十天
	        dAfter = calendar2.getTime(); // 得到前一天的时间
	        
			int userId=TestDataManager.getRandomIntNum(5);
			int sourceId=TestDataManager.getRandomIntNum(5);
			bean.setSourceId(sourceId);
			bean.setBizType(11);//
			bean.setUserId(userId);
			//创建一个商品
			String productId=new DiscountsCallService().createProduct().getString("ProductId");
			bean.setProductId(productId);
			
			bean.setStartTime(dBefore);
			bean.setExpireTime(dAfter);
			
			bean.setAppId("11");
			bean.setRequestId("22");
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("fail", call.getReturnData(), "验证返回ok");
			//验证数据库
			discount_qualification dq=dqWapper.selectTop1(Long.toString(bean.getSourceId()), Integer.toString(bean.getUserId()), bean.getProductId());
			Logger.verifyEquals(dq.getQualificationId(), null, "验证discount_qualification表插入不成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_add_006() {
		Logger.start(false,"反例-新增会员优惠资格-userId为空");
		try {
			Date dNow = new Date(); // 当前时间
	        Date dBefore = new Date();
	        Date dAfter = new Date();
	        
	        Calendar calendar1 = Calendar.getInstance(); // 得到日历
	        calendar1.setTime(dNow);// 把当前时间赋给日历
	        calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
	        dBefore = calendar1.getTime(); // 得到前一天的时间

	        Calendar calendar2 = Calendar.getInstance(); // 得到日历
	        calendar2.setTime(dNow);// 把当前时间赋给日历
	        calendar2.add(Calendar.DAY_OF_MONTH, 10); // 设置为后十天
	        dAfter = calendar2.getTime(); // 得到前一天的时间
	        
			int userId=TestDataManager.getRandomIntNum(5);
			int sourceId=TestDataManager.getRandomIntNum(5);
			bean.setSourceId(sourceId);
			bean.setBizType(1);
		//	bean.setUserId(userId);//userId为空
			//创建一个商品
			String productId=new DiscountsCallService().createProduct().getString("ProductId");
			bean.setProductId(productId);
			
			bean.setStartTime(dBefore);
			bean.setExpireTime(dAfter);
			
			bean.setAppId("11");
			bean.setRequestId("22");
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("fail", call.getReturnData(), "验证返回ok");
			//验证数据库
			discount_qualification dq=dqWapper.selectTop1(Long.toString(bean.getSourceId()), Integer.toString(bean.getUserId()), bean.getProductId());
			Logger.verifyEquals(dq.getQualificationId(), null, "验证discount_qualification表插入不成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_add_007() {
		Logger.start(false,"反例-新增会员优惠资格-productId为空");
		try {
			Date dNow = new Date(); // 当前时间
	        Date dBefore = new Date();
	        Date dAfter = new Date();
	        
	        Calendar calendar1 = Calendar.getInstance(); // 得到日历
	        calendar1.setTime(dNow);// 把当前时间赋给日历
	        calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
	        dBefore = calendar1.getTime(); // 得到前一天的时间

	        Calendar calendar2 = Calendar.getInstance(); // 得到日历
	        calendar2.setTime(dNow);// 把当前时间赋给日历
	        calendar2.add(Calendar.DAY_OF_MONTH, 10); // 设置为后十天
	        dAfter = calendar2.getTime(); // 得到前一天的时间
	        
			int userId=TestDataManager.getRandomIntNum(5);
			int sourceId=TestDataManager.getRandomIntNum(5);
			bean.setSourceId(sourceId);
			bean.setBizType(1);
			bean.setUserId(userId);
//			//创建一个商品
//			String productId=new DiscountsCallService().createProduct().getString("ProductId");
//			bean.setProductId(productId);
			
			bean.setStartTime(dBefore);
			bean.setExpireTime(dAfter);
			
			bean.setAppId("11");
			bean.setRequestId("22");
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("fail", call.getReturnData(), "验证返回ok");
			//验证数据库
			discount_qualification dq=dqWapper.selectTop1(Long.toString(bean.getSourceId()), Integer.toString(bean.getUserId()), bean.getProductId());
			Logger.verifyEquals(dq.getQualificationId(), null, "验证discount_qualification表插入不成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_add_008() {
		Logger.start(false,"反例-新增会员优惠资格-StartTime为空");
		try {
			Date dNow = new Date(); // 当前时间
	        Date dBefore = new Date();
	        Date dAfter = new Date();
	        
	        Calendar calendar1 = Calendar.getInstance(); // 得到日历
	        calendar1.setTime(dNow);// 把当前时间赋给日历
	        calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
	        dBefore = calendar1.getTime(); // 得到前一天的时间

	        Calendar calendar2 = Calendar.getInstance(); // 得到日历
	        calendar2.setTime(dNow);// 把当前时间赋给日历
	        calendar2.add(Calendar.DAY_OF_MONTH, 10); // 设置为后十天
	        dAfter = calendar2.getTime(); // 得到前一天的时间
	        
			int userId=TestDataManager.getRandomIntNum(5);
			int sourceId=TestDataManager.getRandomIntNum(5);
			bean.setSourceId(sourceId);
			bean.setBizType(1);
			bean.setUserId(userId);
			//创建一个商品
			String productId=new DiscountsCallService().createProduct().getString("ProductId");
			bean.setProductId(productId);
			
			//bean.setStartTime(dBefore);
			bean.setExpireTime(dAfter);
			
			bean.setAppId("11");
			bean.setRequestId("22");
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("fail", call.getReturnData(), "验证返回ok");
			//验证数据库
			discount_qualification dq=dqWapper.selectTop1(Long.toString(bean.getSourceId()), Integer.toString(bean.getUserId()), bean.getProductId());
			Logger.verifyEquals(dq.getQualificationId(), null, "验证discount_qualification表插入不成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_add_009() {
		Logger.start(false,"反例-新增会员优惠资格-ExpireTime为空");
		try {
			Date dNow = new Date(); // 当前时间
	        Date dBefore = new Date();
	        Date dAfter = new Date();
	        
	        Calendar calendar1 = Calendar.getInstance(); // 得到日历
	        calendar1.setTime(dNow);// 把当前时间赋给日历
	        calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
	        dBefore = calendar1.getTime(); // 得到前一天的时间

	        Calendar calendar2 = Calendar.getInstance(); // 得到日历
	        calendar2.setTime(dNow);// 把当前时间赋给日历
	        calendar2.add(Calendar.DAY_OF_MONTH, 10); // 设置为后十天
	        dAfter = calendar2.getTime(); // 得到前一天的时间
	        
			int userId=TestDataManager.getRandomIntNum(5);
			int sourceId=TestDataManager.getRandomIntNum(5);
			bean.setSourceId(sourceId);
			bean.setBizType(1);
			bean.setUserId(userId);
			//创建一个商品
			String productId=new DiscountsCallService().createProduct().getString("ProductId");
			bean.setProductId(productId);
			
			bean.setStartTime(dBefore);
			//bean.setExpireTime(dAfter);
			
			bean.setAppId("11");
			bean.setRequestId("22");
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("fail", call.getReturnData(), "验证返回ok");
			//验证数据库
			discount_qualification dq=dqWapper.selectTop1(Long.toString(bean.getSourceId()), Integer.toString(bean.getUserId()), bean.getProductId());
			Logger.verifyEquals(dq.getQualificationId(), null, "验证discount_qualification表插入不成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verifyReturn()
	{
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
		Logger.verifyEquals("ok", call.getReturnData(), "验证返回ok");
		//验证数据库
		discount_qualification dq=dqWapper.selectTop1(Long.toString(bean.getSourceId()), Integer.toString(bean.getUserId()), bean.getProductId());
		Logger.verifyEquals(dq.getSourceId(), bean.getSourceId(), "验证discount_qualification字段SourceId是否正确");
		Logger.verifyEquals(dq.getBizType(), bean.getBizType(), "验证discount_qualification字段BizType是否正确");
		Logger.verifyEquals(dq.getUserId(), bean.getUserId(), "验证discount_qualification字段UserId是否正确");
		Logger.verifyEquals(dq.getProductId(), bean.getProductId(), "验证discount_qualification字段ProductId是否正确");
		
		Logger.verifyEquals(formatter.format(dq.getStartTime()), formatter.format(bean.getStartTime()), "验证discount_qualification字段StartTime是否正确");
		Logger.verifyEquals(formatter.format(dq.getExpireTime()), formatter.format(bean.getExpireTime()), "验证discount_qualification字段ExpireTime是否正确");
	}

	
}